<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'员工列表'),
);
$return['stores']=pdo_getall('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
//员工列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$list=pdo_getslice('jy_yxw_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：前往《营销网》总平台添加员工！
		','total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'html_to'=>'list_html','list'=>array(
			array('type'=>'string','title'=>'姓名','name'=>'title'),
			array('type'=>'string','title'=>'手机号','name'=>'mobile'),
			array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'gangwei','title'=>'岗位名称','type'=>'string'),
			array('name'=>'juese','title'=>'角色','type'=>'radio','options'=>'juese'),
			array('name'=>'add_days','title'=>'入职时间','type'=>'string'),
			array('type'=>'string','title'=>'开户名称','name'=>'bankname','help'=>''),
			array('type'=>'string','title'=>'银行卡号','name'=>'bankcard','help'=>''),
			array('type'=>'string','title'=>'支付宝账户','name'=>'alipay','help'=>''),
			array('type'=>'string','title'=>'微信号','name'=>'wechat','help'=>''),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'添加日期','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','action'=>'jy_yxgz/worker/addmorefee/','title'=>'添加扣费'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'员工名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'storeid','width'=>'15','title'=>'所属门店','type'=>'select','options'=>'stores'),
		),
	);
	$return['juese']=$this->user_juese;
	$return['status']=$this->worker_status;
	$return['sex']=$this->sex;
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxw_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['storeids']=json_decode($item['storeids'],true);
	$item['pcd']=json_decode($item['pcd'],true);
	$item['password']='';
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('type'=>'text','title'=>'工作性质','name'=>'remark','help'=>''),
			array('type'=>'string','title'=>'开户名称','name'=>'bankname','help'=>''),
			array('type'=>'string','title'=>'银行卡号','name'=>'bankcard','help'=>''),
			array('type'=>'string','title'=>'支付宝账户','name'=>'alipay','help'=>''),
			array('type'=>'string','title'=>'微信号','name'=>'wechat','help'=>''),
		)
	);
	$return['sex']=$this->sex;
	$return['status']=$this->worker_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写姓名！','error');
	}
	if(!$postdata['groupid'] || !$postdata['group2id']){
		exi('请选择部门和职位！','error');
	}
	$data=array(
		'remark'=>$postdata['remark'],
		'bankname'=>$postdata['bankname'],
		'bankcard'=>$postdata['bankcard'],
		'add_days'=>$postdata['add_days'],
		'alipay'=>$postdata['alipay'],
		'wechat'=>$postdata['wechat'],
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_yxw_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_yxw_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	//员工列表
	
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'role'=>$this->session('role'));
	if($_RQ['status']!='all'){
		$params['status']=intval($_RQ['status']);
	}
	$return['params']['status']=$_RQ['status'];
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$list=pdo_getall('jy_yxw_worker',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['roletitle']=$this->user_juese[$item['role']];
			$item['statustitle']=$this->worker_status[$item['status']];
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'id'=>'员工ID',
		'title'=>'姓名',
		'mobile'=>'手机号',
		'roletitle'=>'岗位',
		'bankname'=>'开户名称',
		'bankcard'=>'银行卡号',
		'alipay'=>'支付宝账户',
		'wechat'=>'微信号',
		'statustitle'=>'状态',
		'createtime'=>'添加时间',
	);
	model('core/tools')->export2excel($header,$list,'员工数据');
}
if($_RQ['op']=='addmorefee'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$month=strtotime($postdata['timedays']);
		$month=date('Y-m',$month);
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'workerid'=>trim($postdata['workerid']),
			'isadd'=>trim($postdata['isadd']),
			'price'=>trim($postdata['price']),
			'remark'=>trim($postdata['remark']),
			'timedays'=>trim($postdata['timedays']),
			'month'=>$month,
		);
		$data['createtime']=time();
		$this->insert('morefeelog',$data);
		exi('操作成功！','','//list/');
	}
	$workerid=intval($_RQ['id']);
	$return['item']=array('workerid'=>$workerid);
	$return['item']['workertitle']=pdo_getcolumn('jy_yxw_worker',array('id'=>$workerid,'uniacid'=>$_SESSION['uniacid']),'title');
	$return['htmls']=array(
			'type'=>'edit',
			'edit'=>array(
				array('name'=>'workertitle','title'=>'员工','type'=>'copy'),
				array('name'=>'isadd','title'=>'费用类型','type'=>'radio','options'=>'isadd'),
				array('name'=>'price','title'=>'额度','type'=>'string'),
				array('name'=>'remark','title'=>'费用说明','type'=>'string'),
				array('name'=>'timedays','title'=>'日期','type'=>'time','time_type'=>'day'),
			),
	);
	$return['isadd']=array('扣除','增加');
	exi($return);
}